package com.yunhe.sms.service.system.impl;

import com.yunhe.commens.dto.Pagedao;
import com.yunhe.sms.dao.system.RoleDao;
import com.yunhe.sms.dao.system.impl.RoleDaoImpl;
import com.yunhe.sms.entity.system.Role;
import com.yunhe.sms.service.system.RoleService;

import java.util.List;

/**
 * @author LeiPeifeng
 * @version V1.0
 * @date 2017/10/24 0024 10:20
 * @Description: 实现角色RoleService 接口
 */
public class RoleServiceImpl implements RoleService {
    private RoleDao roleDao= new RoleDaoImpl();

    @Override
    public void save(Role role,String premissionIds) {
        Long Roleid = roleDao.save ( role );//返回当前保存用户的id
        if (null != premissionIds && !premissionIds.isEmpty ()){
            String[] split = premissionIds.split ( "," );
/*            Object[][] params = new Object[1][split.length];
            for (int i=0;i<split.length;i++){
                params[0][i] = split[i];
            }
            roleDao.saveRolePremi ( params );*/
            for (String Premiid : split){
                roleDao.saveRolePremi ( Roleid,new Long ( Premiid ) );
            }
        }
    }

    @Override
    public void deleted(String ids) {
        if (null != ids && !ids.isEmpty ()) {
            String[] split = ids.split ( "," );
            for (String id : split) {
                roleDao.deleted ( new Long(id) );//删除角色信息
                roleDao.deletedRolPremi ( new Long(id) );//删除角色相关的权信息
            }
        }
    }

    @Override
    public void update(Role role,String premssionIds) {
        roleDao.update ( role );//更新角色信息
        Long id = role.getId ();//获取角色的id
        roleDao.deletedRolPremi ( id );//删除原有的用户权限信息
        if (null != premssionIds && !premssionIds.isEmpty ()){
            String[] split = premssionIds.split ( "," );
            for (String Premid : split){
                roleDao.saveRolePremi ( id,new Long ( Premid ) );//保存角色权限
            }
        }
    }

    @Override
    public Role find(Long id) {
        return roleDao.find ( id );
    }

    @Override
    public List<Role> findList() {
        return roleDao.findList ();
    }

    @Override
    public List<Role> findListStatu() {
        return roleDao.findListStatu ();
    }

    @Override
    public Pagedao<Role> page(int offset, int limit) {
        return roleDao.page ( offset,limit );
    }
}
